package com.woniuxy.project115.kaer.platform.web;

import com.woniuxy.project115.kaer.platform.interceptor.Loginlnterceptor;
import com.woniuxy.project115.kaer.platform.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private Loginlnterceptor loginlnterceptor;

    @Resource
    private PermissionInterceptor permissionInterceptor;

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**") // 所有接口
//                .allowCredentials(true) // 是否发送 Cookie
////                .allowedOrigins("*") // 支持域
//                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE", })
//                .allowedHeaders("*")
//                .exposedHeaders("*");
//    }

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOrigins("*")
//                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS");
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginlnterceptor)
                .addPathPatterns("/**")//拦截地址
                .excludePathPatterns("/account/login");
//                .excludePathPatterns("/bookInfo/selectCollectRank")
//                .excludePathPatterns("/userInfo/login","/userInfo/sendRegisterCode","/userInfo/sendLoginCode","/userInfo/userLoginByPhone","/userInfo/addUser")
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");//白名单\

        registry.addInterceptor(permissionInterceptor)
                .addPathPatterns("/**")//拦截地址
                .excludePathPatterns("/account/login");
//                .excludePathPatterns("/bookInfo/selectCollectRank")
//                .excludePathPatterns("/userInfo/login","/userInfo/sendRegisterCode","/userInfo/sendLoginCode","/userInfo/userLoginByPhone","/userInfo/addUser")
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");//白名单\
    }
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String s) {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
               return LocalDateTime.parse(s, formatter);
            }
        });
    }
}
